package com.dm.cloud.images.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dm.cloud.images.service.ImagesAppArrangeService;
import com.dm.cloud.tkmybatis.service.BaseMapper;
import com.dm.cloud.images.mapper.ImagesContainerMapper;
import com.dm.cloud.images.service.ImagesContainerService;
import com.dm.cloud.images.common.po.ImagesContainer;
import com.dm.cloud.core.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
   * @author hcx
   * @type ImagesContainerServiceImpl.java
   * @desc ImagesContainer serviceImpl
   */
@Service

@Transactional

public class ImagesContainerServiceImpl implements ImagesContainerService {


    @Autowired
    private ImagesContainerMapper imagesContainerMapper;

    @Autowired
    private ImagesAppArrangeService imagesAppArrangeService;

    @Override
    public BaseMapper getMapper() {
        return imagesContainerMapper;
    }
    public void processBeforeOperation(ImagesContainer entity, BaseOperationEnum operationEnum) throws CustomException {
    }

    public void processAfterOperation(ImagesContainer entity, BaseOperationEnum operationEnum) throws CustomException {
    }


    @Override
    public ImagesContainer findImageContaniner(String nodeId, String containerId) {
        ImagesContainer container=imagesContainerMapper.findImageContaniner(nodeId,containerId);
        return container;
    }

    @Override
    public boolean deleteByNodeIdAndImagesId(String containerId, String nodeId) {
        int x=imagesContainerMapper.deleteByContaniner(nodeId,containerId);
        boolean isOk= x>0?true:false;
        if(isOk){
            imagesAppArrangeService.deleteByNodeIdAndImagesId(containerId,nodeId);
        }
        return isOk;
    }

    @Override
    public List<ImagesContainer> findByAppCode(String appCode) {
        if(StrUtil.isEmpty(appCode)){
            throw new CustomException("应用code不能为空!");
        }
        List<ImagesContainer> containers= imagesContainerMapper.findImageContaninerByAppcode(appCode);
        return containers;
    }

    @Override
    public void clearContanierAppCode(String nodeId, String containerId) {
        imagesContainerMapper.clearContanierAppCode(nodeId,containerId);
        imagesAppArrangeService.deleteByNodeIdAndImagesId(nodeId,containerId);
    }
}
